package com.atguigu.gmall.pms.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuEntity;
import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuMapper;
import com.atguigu.gmall.pms.vo.SaleAttrValueVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.service.SkuAttrValueService;
import org.springframework.util.CollectionUtils;


@Service("skuAttrValueService")
public class SkuAttrValueServiceImpl extends ServiceImpl<SkuAttrValueMapper, SkuAttrValueEntity> implements SkuAttrValueService {

    @Autowired
    private AttrMapper attrMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private  SkuAttrValueMapper attrValueMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuAttrValueEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuAttrValueEntity>()
        );

        return new PageResultVo(page);
    }
    @Override
    public List<SkuAttrValueEntity> querySearchSkuAttrValueByCidAndSkuId(Long cid, Long skuId) {
        //根据cid查询索引类型的规格参数
        List<AttrEntity> attrEntities = this.attrMapper.selectList( new QueryWrapper<AttrEntity>().eq( "category_id", cid ).eq( "search_type", 1 ) );
        if (CollectionUtils.isEmpty( attrEntities )){
            return null;
        }
        List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());
        //根据spuId和attrIds查询规格参数及值
        return this.list(new QueryWrapper<SkuAttrValueEntity>(  ).eq( "sku_id",skuId ).in( "attr_id" ,attrIds));
    }

    @Override
    public List<SaleAttrValueVo> querySaleAttrsBySpuId(Long spuId) {
        //根据spuId查询所有的sku
        List<SkuEntity> skuEntities = this.skuMapper.selectList( new QueryWrapper<SkuEntity>().eq( "spu_id", spuId ) );
        if (CollectionUtils.isEmpty( skuEntities )) {
            return null;
        }
        //获取skuId集合
        List<Long> skuIds = skuEntities.stream().map( SkuEntity::getId ).collect( Collectors.toList() );
        List<SkuAttrValueEntity> skuAttrValueEntities = this.list( new QueryWrapper<SkuAttrValueEntity>().in( "sku_id", skuIds ) );

        //根据attrId对集合数据分组,List<SkuAttrValueEntity>是attrid对应的数据
        Map<Long, List<SkuAttrValueEntity>> map = skuAttrValueEntities.stream().collect( Collectors.groupingBy( SkuAttrValueEntity::getAttrId ) );
       //遍历分组后的map,把每一个kv结构转换成vo
        List<SaleAttrValueVo> saleAttrValueVos=new ArrayList<>(  );
        map.forEach( (attrId,skuAttrValueEntitiesList) -> {
            SaleAttrValueVo saleAttrValueVo=new SaleAttrValueVo();
            saleAttrValueVo.setAttrId( attrId );
            saleAttrValueVo.setAttrName( skuAttrValueEntitiesList.get( 0 ).getAttrName() );
            saleAttrValueVo.setAttrValues( skuAttrValueEntitiesList.stream().map( SkuAttrValueEntity::getAttrValue ).collect( Collectors.toSet() ) );
        } );

        return saleAttrValueVos;
    }

    @Override
    public String queryMappingBySkuIds(Long spuId) {
        //根据spuId查询所有的sku
        List<SkuEntity> skuEntities = this.skuMapper.selectList( new QueryWrapper<SkuEntity>().eq( "spu_id", spuId ) );
        if (CollectionUtils.isEmpty( skuEntities )) {
            return null;
        }
        //获取skuId集合
        List<Long> skuIds = skuEntities.stream().map( SkuEntity::getId ).collect( Collectors.toList() );

        //查询映射关系
        List<Map<String, Object>> maps = this.attrValueMapper.queryMappingBySkuIds( skuIds );
        if (CollectionUtils.isEmpty( maps )) {
            return null;
        }
        // 把map的list集合 转换成map集合
        Map<String, Long> mappingMap = maps.stream().collect( Collectors.toMap( map -> map.get( "attr_values" ).toString(), map -> (Long) map.get( "sku_id" ) ) );

        return JSON.toJSONString(mappingMap);
    }
}